package cn.anxingongyou.anxin.controller;

import cn.anxingongyou.anxin.entity.UserEmployerInfo;
import cn.anxingongyou.anxin.entity.UserParent;
import cn.anxingongyou.anxin.service.UserEmployerInfoService;
import cn.anxingongyou.anxin.service.UserEmployerRepository;
import cn.anxingongyou.anxin.service.UserParentService;
import cn.anxingongyou.anxin.utils.RedisUtil;
import cn.anxingongyou.anxin.utils.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserEmployerInfoController {

    private final UserEmployerInfoService userEmployerInfoService;
    private final UserParentService userParentService;
    private final UserEmployerRepository userEmployerRepository;
    @Autowired
    public UserEmployerInfoController(UserEmployerInfoService userEmployerInfoService, UserParentService userParentService, UserEmployerRepository userEmployerRepository) {
        this.userEmployerInfoService = userEmployerInfoService;
        this.userParentService = userParentService;
        this.userEmployerRepository = userEmployerRepository;
    }

    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("添加雇主信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/employer/add",method = RequestMethod.POST)
    public Map addEmployer(
            @RequestBody @ApiParam(value = "雇主信息", required = true) UserEmployerInfo userEmployerInfo
            ){
        userEmployerInfo.setCreated(new Date());
        userEmployerInfo.setUpdated(new Date());
        int res = userEmployerInfoService.insert(userEmployerInfo);
        userEmployerRepository.save(userEmployerInfo);
        if(res > 0) {
            return Result.instance(201,"添加成功").toJsonMap();
        } else {
            return Result.instance(500,"添加失败").toJsonMap();
        }
    }

    @ApiOperation("删除雇主信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "删除成功"),
            @ApiResponse(code = 500,message = "删除失败")
    })
    @RequestMapping(value = "/employer/del/{id}",method = RequestMethod.GET)
    public Map delEmployer(
            @PathVariable @ApiParam(value = "雇主主键", required = true) Long id
    ){
        int res = userEmployerInfoService.deleteByPrimaryKey(id);
        userEmployerRepository.deleteById(id);
        if(res > 0) {
            return Result.instance(204,"删除成功").toJsonMap();
        } else {
            return Result.instance(500,"删除失败").toJsonMap();
        }
    }

    @ApiOperation("更新雇主信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/employer/update",method = RequestMethod.POST)
    public Map updateEmployer(
            @RequestBody @ApiParam(value = "雇主信息,必传主键", required = true) UserEmployerInfo userEmployerInfo
    ){
        String key = "employer#"+ userEmployerInfo.getId().toString();
        if(redisUtil.hasKey(key)){
            // 存在缓存 删除缓存
            redisUtil.del(key);
        }
        int res = userEmployerInfoService.updateByPrimaryKeySelective(userEmployerInfo);
        UserEmployerInfo result = userEmployerInfoService.selectOneByPrimaryKey(userEmployerInfo.getId());
        userEmployerRepository.save(result);
        if(res > 0) {
            redisUtil.set(key, result);
            return Result.instance(204,"更新成功").toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("企业信息认证通过")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/employer/isauth",method = RequestMethod.POST)
    public Map isAuthEmployer(
            @RequestBody @ApiParam(value = "雇主信息,必传主键", required = true) UserEmployerInfo userEmployerInfo
    ){
        String key = "employer#"+ userEmployerInfo.getId().toString();
        if(redisUtil.hasKey(key)){
            // 存在缓存 删除缓存
            redisUtil.del(key);
        }
        int res = userEmployerInfoService.updateByPrimaryKeySelective(userEmployerInfo);

        UserEmployerInfo result = userEmployerInfoService.selectOneByPrimaryKey(userEmployerInfo.getId());
        userEmployerRepository.save(result);

        UserParent userParent = new UserParent();
        userParent.setId(userEmployerInfo.getId());
        userParent.setIsEnterpriseCertification(1);
        userParentService.updateByPrimaryKeySelective(userParent);
        if(res > 0) {
            return Result.instance(204,"设置成功").toJsonMap();
        } else {
            return Result.instance(500,"操作失败").toJsonMap();
        }
    }
    @ApiOperation("企业信息认证未通过")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/employer/isnotauth",method = RequestMethod.POST)
    public Map isNotAuthEmployer(
            @RequestBody @ApiParam(value = "雇主信息,必传主键", required = true) UserEmployerInfo userEmployerInfo
    ){
        String key = "employer#"+ userEmployerInfo.getId().toString();
        if(redisUtil.hasKey(key)){
            // 存在缓存 删除缓存
            redisUtil.del(key);
        }
        int res = userEmployerInfoService.updateByPrimaryKeySelective(userEmployerInfo);

        UserEmployerInfo result = userEmployerInfoService.selectOneByPrimaryKey(userEmployerInfo.getId());
        userEmployerRepository.save(result);


        if(res > 0) {
            return Result.instance(204,"设置成功").toJsonMap();
        } else {
            return Result.instance(500,"操作失败").toJsonMap();
        }
    }

    @ApiOperation("api更新雇主信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/api/employer/update",method = RequestMethod.POST)
    public Map apiUpdateEmployer(
            @RequestBody @ApiParam(value = "雇主信息,必传主键", required = true) UserEmployerInfo userEmployerInfo
    ){
        String key = "employer#"+ userEmployerInfo.getId().toString();
        if(redisUtil.hasKey(key)){
            // 存在缓存 删除缓存
            redisUtil.del(key);
        }
        int res = userEmployerInfoService.updateByPrimaryKeySelective(userEmployerInfo);
        userEmployerInfo = userEmployerInfoService.selectOneByPrimaryKey(userEmployerInfo.getId());
        userEmployerRepository.save(userEmployerInfo);
        if(res > 0) {
            redisUtil.set(key,userEmployerInfo);
            return Result.<UserEmployerInfo>instance(204,"更新成功",userEmployerInfo).toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("获取信息通过")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/employer/findByEmployer",method = RequestMethod.POST)
    public Map findOneEmployer(
            @RequestBody @ApiParam(value = "雇主信息", required = true) UserEmployerInfo userEmployerInfo
    ){
        UserEmployerInfo userEmployerInfo1 = userEmployerInfoService.selectOne(userEmployerInfo);
        if(userEmployerInfo1 != null) {
            return Result.<UserEmployerInfo>instance(200,"获取成功",userEmployerInfo1).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("api获取信息通过")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/api/employer/findByEmployer",method = RequestMethod.POST)
    public Map apiFindOneEmployer(
            @RequestBody @ApiParam(value = "雇主信息", required = true) UserEmployerInfo userEmployerInfo
    ){
        UserEmployerInfo userEmployerInfo1 = userEmployerInfoService.selectOne(userEmployerInfo);
        if(userEmployerInfo1 != null) {
            return Result.<UserEmployerInfo>instance(200,"获取成功",userEmployerInfo1).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取雇主信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/employer/findOneById/{id}",method = RequestMethod.GET)
    public Map findOneById(
            @PathVariable @ApiParam(value = "雇主主键", required = true) Long id
    ){
        UserEmployerInfo userEmployerInfo = userEmployerInfoService.selectOneByPrimaryKey(id);

        if(userEmployerInfo != null) {
            userEmployerRepository.save(userEmployerInfo);
            return Result.<UserEmployerInfo>instance(200,"获取成功",userEmployerInfo).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("api获取雇主信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/api/employer/findOneById/{id}",method = RequestMethod.GET)
    public Map apiFindOneById(
            @PathVariable @ApiParam(value = "雇主主键", required = true) Long id
    ){
        String key = "employer#"+ id.toString();
        if(redisUtil.hasKey(key)){
            // 存在缓存
            UserEmployerInfo userEmployerInfo = (UserEmployerInfo) redisUtil.get(key);
            if(userEmployerInfo != null) {
                return Result.<UserEmployerInfo>instance(200,"获取成功",userEmployerInfo).toJsonMap();
            } else {
                return Result.instance(500,"获取失败").toJsonMap();
            }
        } else {
            // 不存在缓存
            UserEmployerInfo userEmployerInfo = userEmployerInfoService.selectOneByPrimaryKey(id);
            if(userEmployerInfo != null) {
                redisUtil.set(key,userEmployerInfo);
                return Result.<UserEmployerInfo>instance(200,"获取成功",userEmployerInfo).toJsonMap();
            } else {
                return Result.instance(500,"获取失败").toJsonMap();
            }
        }
    }

    @ApiOperation("获取所有雇主")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/employer/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectEmployerByEmployer(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "招聘者信息", required = true)  UserEmployerInfo userEmployerInfo){
        PageInfo<UserEmployerInfo> pageInfo = userEmployerInfoService.page(pageNum, pageSize, userEmployerInfo);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

    @ApiOperation("筛选雇主")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/employer/search/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map searchEmployerByEmployer(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "招聘者信息", required = true)  UserEmployerInfo userEmployerInfo){
        PageInfo<UserEmployerInfo> pageInfo = userEmployerInfoService.search(pageNum, pageSize, userEmployerInfo);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

    @ApiOperation("api筛选招聘者")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/api/employer/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map apiSelectEmployerByEmployer(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "招聘者信息", required = true)  UserEmployerInfo userEmployerInfo){
        PageInfo<UserEmployerInfo> pageInfo = userEmployerInfoService.page(pageNum, pageSize, userEmployerInfo);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

}
